package com.coocaa.ops.admin.v1.poster.provider.impl;

import com.coocaa.ops.admin.v1.poster.biz.impl.GlobalPictureBizImpl;
import com.coocaa.ops.admin.v1.poster.convert.PosterSaveConverter;
import com.coocaa.ops.admin.v1.poster.service.IGlobalPictureUpdateProvider;
import com.coocaa.ops.common.core.biz.poster.entity.GlobalPictureEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Objects;

/**
 * @author bjh
 * @description 素材图片表-更新provider接口实现
 * @date 2020-06-13 10:50:35
 */
@Service
public class GlobalPictureUpdateProviderImpl implements IGlobalPictureUpdateProvider {
    @Autowired
    private GlobalPictureBizImpl posterBiz;

    @Override
    public void save(GlobalPictureEntity entity, Integer userId) {
        //保存前处理
        PosterSaveConverter.handleBeforeSave(entity);
        if (null == entity.getId()) {
            entity.setCreator(userId);
            posterBiz.add(entity);
        } else {
            validUrl(entity);
            // 设置  修改人
            entity.setModifier(userId);
            posterBiz.updateById(entity);
        }
    }

    /*************** ===私有方法=== ********************* ===start=== ***************************************/

    private void validUrl(GlobalPictureEntity entity) {
        GlobalPictureEntity oldEntity = posterBiz.queryById(entity.getId());

        Assert.isTrue(Objects.equals(oldEntity.getPictureUrl(), entity.getPictureUrl()), "不能修改图片url");

    }
}
